package com.kamistoat.meimeimall.meimeimallproduct.service.impl;


import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.common.utils.Query;
import com.kamistoat.meimeimall.meimeimallproduct.dao.BrandDao;
import com.kamistoat.meimeimall.meimeimallproduct.dao.CategoryDao;
import com.kamistoat.meimeimall.meimeimallproduct.entity.BrandEntity;
import com.kamistoat.meimeimall.meimeimallproduct.entity.CategoryEntity;
import com.kamistoat.meimeimall.meimeimallproduct.dao.CategoryBrandRelationDao;
import com.kamistoat.meimeimall.meimeimallproduct.entity.CategoryBrandRelationEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.kamistoat.meimeimall.meimeimallproduct.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    // 补齐Entity时需要使用这两个Dao来执行跨域查询
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    BrandDao brandDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 我们自己编写的方法，根据categoryBrandRelation中的brandId和catelogId字段，将剩下的字段补齐
     *
     * @param categoryBrandRelation 一个Entity，但是前端只会传来两个字段，这个Entity中剩下的字段是空的
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        // 先把剩下字段所在的Entity查询出来。
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        // 然后将其字段取出，放到categoryBrandRelation中
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        // 调用自身Service的save方法，将categoryBrandRelation保存
        this.save(categoryBrandRelation);
    }

    /**
     * 我们自己编写的方法，更新自身的冗余字段
     * 传输来的参数，应该对应CategoryBrandRelationEntity中的某些字段
     *
     * @param brandId 主键，用于定位冗余字段的brandId
     * @param name    冗余字段brandName
     */
    @Override
    public void updateCBRelation_Brand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        // 用条件更新来更新
        this.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    /**
     * 我们自己编写的方法，更新自身的冗余字段
     *
     * @param catId 主键，定位冗余字段的catId
     * @param name  冗余字段categoryName
     */
    @Override
    public void updateCBRelation_Category(Long catId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(catId);
        categoryBrandRelationEntity.setCatelogName(name);
        this.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
    }

    /**
     * 自己编写的方法，根据三级菜单查询对应的品牌信息
     *
     * @param catId 三级菜单ID
     * @return 列表
     */
    @Override
    public List<BrandEntity> getBrandByCatId(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities =
                baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        if (categoryBrandRelationEntities.size() == 0) {
            return Collections.emptyList();
        } else {
            List<BrandEntity> brandEntityList = categoryBrandRelationEntities.stream().map((categoryBrandRelationEntity) -> {
                return brandDao.selectById(categoryBrandRelationEntity.getBrandId());
            }).collect(Collectors.toList());
            return brandEntityList;
        }
    }

}